package com.shjl.kechuang.utils;

import cn.hutool.core.bean.BeanUtil;
import com.shjl.kechuang.domain.pojo.KyTechSme;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @title: ListFilesRecursively
 * @author: wywang
 * @date: 2025/04/07
 * @description:
 **/
@Slf4j
@Component
public class ListFilesRecursively {

    @Autowired
    private PdfReader pdfReader;

    /**
     * 输入PDF文件路径
      * @param filePath
     */
    public  void InputPDF(String filePath) {

        File directory = new File(filePath);

        if (directory.exists() && directory.isDirectory()) {
            listFiles(directory);
        } else {
            log.info("指定的路径不是一个有效的文件夹！");
        }
    }


    /**
     * 递归遍历文件夹下的所有文件
     */
    private  void listFiles(File file) {
        if (file.isDirectory()) {
            System.out.println("文件夹: " + file.getAbsolutePath());
            File[] subFiles = file.listFiles();
            if (subFiles != null) {
                for (File subFile : subFiles) {
                    listFiles(subFile); // 递归调用
                }
            }
        } else {
            String fileName = file.getAbsolutePath();
            System.out.println("文件: " + fileName);

            String[] split = fileName.split("\\\\");
            // 获取省份名称
            String provinceName = split[split.length - 2];
            String baseName = split[split.length - 1];

            // 从文件名称中获取年份和批次
            KyTechSme kyTechSme = extractYearAndBatch(baseName);
            if(BeanUtil.isEmpty(kyTechSme)){
                log.info("未能匹配到年份和批次：{}", fileName);
                return;
            }
            kyTechSme.setProvince(provinceName);
            kyTechSme.setDr(0);
            kyTechSme.setUpdateTime(new Date());

            // 调用PdfReader的readerPDFtoSQL方法,读取PDF文件
            log.info("开始读取PDF文件：{}", fileName);
            pdfReader.readerPDFtoSQL(fileName, kyTechSme);


        }
    }


    /**
     * 从文件名称中提取年份和批次
     */
    private static KyTechSme extractYearAndBatch(String input) {
        // 正则表达式匹配年份和批次，兼容汉字和阿拉伯数字
        String regex = "([\\u4e00-\\u9fa5]+)(\\d{4})年第(一|二|三|四|五|六|七|八|九|十|[1-9]\\d*)批(.+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {

            String year = matcher.group(2); // 年份
            String batchNumber = convertChineseNumberToArabic(matcher.group(3)); // 批次号（转换为阿拉伯数字）

            // 构建KyTechSme对象
            return KyTechSme.builder()
                    .pubYear(Integer.parseInt(year))
                    .batch(batchNumber)
                    .build();

        } else {
            log.info("未能匹配到年份和批次：{}", input);
            return null;
        }
    }

    /**
     * 将中文数字转换为阿拉伯数字
     */
    private static String convertChineseNumberToArabic(String chineseNumber) {
        switch (chineseNumber) {
            case "一":
                return "1";
            case "二":
                return "2";
            case "三":
                return "3";
            case "四":
                return "4";
            case "五":
                return "5";
            case "六":
                return "6";
            case "七":
                return "7";
            case "八":
                return "8";
            case "九":
                return "9";
            case "十":
                return "10";
            default:
                // 如果已经是阿拉伯数字，则直接返回
                if (chineseNumber.matches("\\d+")) {
                    return chineseNumber;
                }
                return "未知";
        }
    }
}
